import subprocess
import re

pkgName = "com.android.vending"
ps = subprocess.Popen("adb shell dumpsys batterystats", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
uid = ""
uid_tag = False
power_tag = False
uid_re = '=(.+?):"{}"'.format(pkgName)
while True:
    line = ps.stdout.readline().decode('utf-8')
    if line:
        if "Battery History" in line:
            uid_tag = True
        if "Estimated power use (mAh):" in line:
            power_tag = True
    else:
        break
 
    if uid_tag:
        temp = re.findall('=(.+?):"{}"'.format(pkgName), line)
        if len(temp) > 0:
            uid = temp[0]
            print(uid)
            uid_tag = False
    if power_tag:
        temp = re.findall("Uid {}: (.+?) ".format(uid), line)
        if len(temp) > 0:
            print(temp[0])
            power_tag = False